<?php

namespace App\Http\Services;

use App\Http\Model\AudioApi;
use App\Http\Model\Config;
use App\Http\Model\Gift;
use App\Http\Model\Hint;
use App\Http\Model\NewYear;
use App\Http\Model\Room;
use App\Http\Model\Union;
use App\Http\Model\User;

class NewYearServices extends CommonServices
{
    private $config_model;
    private $gift_model;
    private $user_model;
    private $newYear_model;
    private $audioApi;
    private $room_model;


    public function __construct()
    {
        $this->config_model = new Config();
        $this->gift_model = new Gift();
        $this->user_model = new User();
        $this->audioApi = new AudioApi();
        $this->newYear_model = new NewYear();
        $this->room_model = new Room();
    }


    public function cacheGift($gift_id)
    {
        return $this->newYear_model->cacheGift($gift_id);
    }

    public function giftToNewUser($gift_id, $to_user_id_arr, $score)
    {
        $cache_gift = $this->newYear_model->cacheGift('');
        foreach ($to_user_id_arr as $v) {

            $this->newYear_model->UserGiftIncNum($v, $gift_id, $score);
            //获取用户最少数量
            $user_score = $this->newYear_model->getUserScore($v);
            $all_score = $this->newYear_model->getUserGiftMin($v, $cache_gift);
            $diff_score = min($all_score) - $user_score < 0 ? 0 : min($all_score) - $user_score;
            $this->newYear_model->incUserRank($v, $diff_score);
        }
        return true;
    }

    public function getUserGiftNum($user_id)
    {
        $cache_gift = $this->newYear_model->cacheGift('');
        $all_score = $this->newYear_model->getUserGiftMin($user_id, $cache_gift);
        $gift_info = $this->gift_model->getGiftInfoToIds(explode(',', $cache_gift));
        foreach ($gift_info as $k => &$v) {
            $v['score'] = $all_score[$v['gift_id']] ?? 0;
        }
        $data['gift_info'] = $gift_info;
        $data['min_score'] = min($all_score) ?? 0;
        $data['user_info'] = $this->user_model->getUserInfoByApi($user_id);

        $all_rank = $this->newYear_model->getGiftNumRank();
        $rank = array_keys($all_rank);
        $data['user_rank'] = -1;//未上榜
        $data['diff_score'] = 0;
        if (in_array($user_id, $rank)) {
            $data['user_rank'] = array_flip($rank)[$user_id] + 1 ?? -1;
        }
        if ($data['user_rank'] == -1 && !empty($all_rank)) {
            $data['diff_score'] = $all_rank[$rank[count($rank) - 1]] - $data['min_score'] ?? 0;
        }
        $data['is_receive'] = 0;//未領取
        $log = $this->newYear_model->getReceive($user_id);
        if ($log) {
            $data['is_receive'] = 1;//已領取
        }
        return $data;
    }

    public function getRank()
    {
        $cache_gift = $this->newYear_model->cacheGift('');
        $rank = array_keys($this->newYear_model->getGiftNumRank());
        $data = [];
        foreach ($rank as $k => $v) {
            $all_score = $this->newYear_model->getUserGiftMin($v, $cache_gift);
            $data[$k]['user_info'] = $this->user_model->getUserInfoByApi($v);
            $nick = $data[$k]['user_info']['nickname'] ?? '';
            $data[$k]['user_info']['username'] = mb_substr($nick, 0, 9, 'utf-8');
            $data[$k]['gift_info'] = $all_score;
            $data[$k]['min_score'] = min($all_score);
            $data[$k]['user_rank'] = $k + 1;
        }
        return $data;
    }

    public function clickGift($user_id, $gift_id)
    {
        $data = [];
        $msg = '失败';
        $code = Hint::ERROR;
        $config = $this->config_model->getShareGoodsConfig(14);
        do {
            if (empty($gift_id)) {
                break;
            }

            $log = $this->newYear_model->getReceive($user_id);
            if ($log) {
                $msg = '您已领取奖励';
                $code = Hint::ERROR;
                break;
            }
            $score = $this->newYear_model->getUserGiftMin($user_id, $config['new_year_gift']['value']);
            if (array_sum($score) < 2020) {
                $msg = '您的福袋数量未达到领取条件。';
                $code = Hint::ERROR;
                break;
            }
            $res = false;
            if ($gift_id == $config['new_year_ring']['value']) {
                //戒指
                $res = $this->audioApi->sendUsedMall($user_id, $gift_id, 1, 7);
            } else if ($gift_id == $config['new_year_seat']['value']) {
                //座驾
                $res = $this->audioApi->sendUsedMall($user_id, $gift_id, 2, 3);
            }
            if (!$res) {
                $msg = '领取失败';
                $code = Hint::ERROR;
                break;
            }
            $this->newYear_model->receiveGift($user_id, $gift_id);
            $msg = '成功';
            $code = Hint::SUCCESS;
        } while (false);
        return compact('code', 'msg', 'data');
    }

    public function getHowe()
    {
        $config = $this->config_model->getShareGoodsConfig(14);
        $howe_ids = $config['new_year_howe']['value'];
        dd($howe_ids);
        $data = [];
        foreach (explode(',', $howe_ids) as $k => $v) {
            $data[$k] = $this->user_model->getUserInfoByApi($v);
            $data[$k]['username'] = substr($data[$k]['nickname'], 0, 15);
        }
        return $data;
    }

    public function getYearlyReport($user_id)
    {
        return $this->audioApi->yearlyReport($user_id);
    }


    public function createPkRank()
    {
        //获取pk房间和时间段
        $rooms_list = $this->room_model->getPkRoom();
        //房间
        foreach ($rooms_list as $v) {
            $room_ids = [];
            switch ($v['type']) {
                case 4:
                    $this->room_model->setRoomWinFailDraw($v['red_room_id'], 'win');
                    $this->room_model->setRoomWinFailDraw($v['blue_room_id'], 'fail');
                    $red_score = 4;
                    $blue_score = 1;
                    break;
                case 5:
                    $this->room_model->setRoomWinFailDraw($v['blue_room_id'], 'win');
                    $this->room_model->setRoomWinFailDraw($v['red_room_id'], 'fail');
                    $red_score = 1;
                    $blue_score = 4;
                    break;
                case 6:
                    $this->room_model->setRoomWinFailDraw($v['blue_room_id'], 'draw');
                    $this->room_model->setRoomWinFailDraw($v['red_room_id'], 'draw');
                    $red_score = 2;
                    $blue_score = 2;
                    break;
                default:
                    $red_score = 0;
                    $blue_score = 0;
            }
            $this->room_model->setRoomPkRank($v['red_room_id'], $red_score);
            $this->room_model->setRoomPkRank($v['blue_room_id'], $blue_score);
            //查询用户
            if ($v['type'] < 4) continue;
            $start_time = $v['close_time'] - $v['pk_time'];
            $end_time = $v['close_time'];
            $room_ids[] = $v['red_room_id'];
            $room_ids[] = $v['blue_room_id'];
            $diamond_log = $this->gift_model->getPkGift($room_ids, $start_time, $end_time);
            if (empty($diamond_log)) continue;
            foreach ($diamond_log as $vv) {
                $this->newYear_model->setUserPkRank($vv['from_user_id'], $vv['gift_price'] * $vv['gift_num']);
            }
        }
        return true;
    }

    public function getPkRank($user_id, $c_type)
    {
        $i = $j = 0;
        $u_data = [];
        $r_data = [];
        $data = [];
        $user_rank_data = [];
        $user_rank_data['diff_value'] = 0;
        $user_rank_data['desc'] = '距上榜';
        $user_rank_data['rank'] = -1;
        switch ($c_type) {
            case  0:
                $user_rank = $this->newYear_model->getUserRank();
                $user_info = $this->user_model->getUserInfoByApi($user_id);
                $user_rank_data['user_info'] = $user_info;
                foreach ($user_rank as $k1 => $v1) {
                    $u_data[$i]['user_info'] = $this->user_model->getUserInfoByApi($k1);
                    $u_data[$i]['score'] = $v1;
                    $u_data[$i]['desc'] = '距上名';
                    if ($i == 0) {
                        $u_data[$i]['desc'] = '战神';
                        $u_data[$i]['diff_value'] = '';
                    } else {
                        $u_data[$i]['diff_value'] = $u_data[$i - 1]['score'] - $u_data[$i]['score'];
                    }
                    if ($k1 == $user_id) {
                        $user_rank_data['diff_value'] = $u_data[$i]['diff_value'];
                        $user_rank_data['desc'] = $u_data[$i]['desc'];
                        $user_rank_data['rank'] = $i + 1;
                    }
                    $i++;
                }
                if ($user_rank_data['rank'] == -1) {
                    $u_score = $this->newYear_model->getUserPkScore($user_id) ?? 0;
                    $user_rank_data['diff_value'] = $u_data[count($u_data) - 1]['score'] - $u_score;
                }
                $data['all_rank'] = $u_data;
                $data['user_rank'] = $user_rank_data;
                break;
            case  1:
                $rooms_rank_list = $this->room_model->getRoomPkRank();
                foreach ($rooms_rank_list as $k1 => $v1) {
                    $r_data[$j]['room_info'] = $this->room_model->getRoomInfoToRoomId($k1);
                    $r_data[$j]['score'] = $v1;
                    $r_data[$j]['win'] = $this->room_model->getRoomWin($k1, 'win') ?? 0;
                    $r_data[$j]['fail'] = $this->room_model->getRoomWin($k1, 'fail') ?? 0;
                    $r_data[$j]['draw'] = $this->room_model->getRoomWin($k1, 'draw') ?? 0;
                    $j++;
                }
                $data = $r_data;
                break;
        }
        return $data;
    }


    public function awardMall()
    {
        //用户
        $user_rank = $this->newYear_model->getUserRank();
        $user_ids = array_slice(array_keys($user_rank), 0, 3);
        $day = [15, 7, 7];
        $config = $this->config_model->getShareGoodsConfig(16);//241
        if (!empty($user_ids)) {
            foreach ($user_ids as $k => $user_id) {
                $time_limit = $day[$k];
                if ($k == 0) {
                    $this->audioApi->sendProp($user_id, $config['prop_gift_one_id']['value'], 1);
                } else {
                    $this->audioApi->sendProp($user_id, $config['prop_gift_two_id']['value'], 1);
                }
                if ($k < 2) {
                    $this->audioApi->sendMedal($user_id, $config['pk_medal']['value'], $time_limit);
                }
                $this->audioApi->sendUsedMall($user_id, $config['pk_headwear']['value'], 1, $time_limit);
                $this->audioApi->sendUsedMall($user_id, $config['pk_bubble']['value'], 3, $time_limit);
            }
        }
        $rooms_rank_list = $this->room_model->getRoomPkRank();
        $room_ids = array_slice(array_keys($rooms_rank_list), 0, 3);
        if (!empty($room_ids)) {
            $num = [5, 3, 1];
            foreach ($room_ids as $k1 => $v1) {
                $room_info = $this->room_model->getRoomInfoToRoomId($v1);
                $this->audioApi->sendProp($room_info['user_id'], $config['prop_id']['value'], $num[$k1]);
            }
        }
        return true;
    }
}